<?php
include_once(BASE_FRAMEWORK . '/Debug.class.php');
class Login
{
    var $Name_username;
    var $Name_password;
    var $Name_ingresar;

    var $conn;

    var $Session_Starteada = 0;

    var $Aceptado = 'index.php';

    var $Archivo_login;

    function Login()
    {
        $this->Session_Start();
        $this->Archivo_login = 'index.php?page=Login';
    }
    function ErrorVal()
    {
        return '1';
    }
    function varError()
    {
        return 'error';
    }
    function Name_username()
    {
        return 'name_username';
    }
    function Name_password()
    {
        return 'name_password';
    }
    function Name_pk()
    {
        return 'name_pk';
    }
    function Name_ingresar()
    {
        return 'name_ingresar';
    }
    function Session_Start()
    {
        if (!$this->Session_Starteada)
        {
            session_start();
            $this->Session_Starteada = 1;
        }
    }
    function Get_Session($VARS = array(), $atributos = array(), $no_return = 0)
    {
        $this->Session_Start();
        $vacio = (count($VARS) == 0);

        if (!$vacio)
        {
            $this->Unset_session();
        } elseif (!empty($_SESSION['session_login']))
        {
            $this->Set_Session();
            return;
        }
        $this->Name_username = $VARS[$this->Name_username()];
        $this->Name_password = $VARS[$this->Name_password()];
        $this->PK            = $VARS[$this->Name_pk()];

        if (empty($this->Name_username) || empty($this->Name_password))
        {
            $this->Login_Error($no_return);
        }

        if (is_array($atributos['guardar']))
        {
            if (empty($atributos['campo_pk']))
            {
                die("Debe setear el campo Primary Key de la Tabla.");
            }
            $guardar = $atributos['campo_pk'] . ", ";
            foreach ($atributos['guardar'] as $i => $campo)
            {
                $guardar .= $campo;
                if ($i < (count($atributos['guardar']) - 1))
                {
                    $guardar .= ', ';
                }
            }
        } else {
            $guardar = $atributos['name_username'];
        }

        if (!empty($atributos['tabla']))
        {
            $this->Tabla = $atributos['tabla'];
            $this->Sql_query = "SELECT ".$atributos['campo_password'].", ".$guardar." FROM ".$this->Tabla;
        } elseif (!empty($atributos['sql_query']))
        {
            $this->Sql_query = $atributos['sql_query'];
        } else {
            Debug::Mostrar('No existe Tabla ni Consulta SQL con la cual corroborar los datos, verifique los atributos');
            exit();
        }
        $this->Sql_query .= " WHERE ".$atributos["campo_username"]." = '".$this->Name_username."'";

        $query       = @mysql_query($this->Sql_query, $this->conn);
        $error       = @mysql_error($this->conn);
        if (!empty($error))
        {
            Debug::Mostrar("Se ha producido un error en una consulta a la Base de Datos - Error 1: Get_Login()<br>".$this->sql_query."<br>".$error);
            exit();
        }
        $row = @mysql_fetch_assoc($query);

        if ($row[$atributos['campo_password']] != $this->Name_password)
        {
            $this->Login_Error($no_return);
        }

        foreach ($row as $campo => $valor)
        {
            if ($campo == $this->Name_Password())
            {
                continue;
            }
            if ($campo == $attributos['campo_pk'])
            {
                $_SESSION['campo_pk'] = $valor;
            } else {
                $_SESSION[$campo] = $valor;
            }
        }

        $_SESSION['session_login'] = time();
          $this->Set_Session($atributos);

        $this->Login_Aceptado();
    }
    function Set_Session($atributos = '')
    {
        if (!empty($atributos))
        {
            $_SESSION['login_attributos'] = $atributos;
        }
        foreach ($_SESSION as $campo => $valor)
        {
            $this->$campo = $valor;
        }
    }
    function Unset_session()
    {
           $this->Session_Start();
        foreach ($_SESSION as $campo => $valor)
        {
            unset($this->$campo);
            unset($_SESSION[$campo]);
        }
    }
    function Logout()
    {
        $this->Unset_session();
         header('Location: '.$this->Archivo_login);
        exit();
    }
    function Login_Aceptado()
    {
        if ($_REQUEST['return_path'] != '')
        {
            $this->Aceptado = $_REQUEST['return_path'];
        }
         header('Location: '.$this->Aceptado);
        exit();
    }
    function Login_Error($no_return = 0)
    {
        $return_path = '&return_path='.urlencode($_SERVER['REQUEST_URI']);
        if ($no_return)
        {
            $return_path = '';
        }
        if (eregi('\?', $_SERVER['REQUEST_URI']) || eregi('\?', $this->Archivo_login))
        {
            $char = '&';
        } else {
            $char = '?';
        }

         header('Location: '.$this->Archivo_login . $char . $this->varError() . '=' . $this->ErrorVal() . $return_path);
        exit();
    }
    function SessionExpire()
    {
        return empty($_SESSION['session_login']);
    }
    function IsError()
    {
        return $_REQUEST[Login::varError()] == Login::ErrorVal();
    }
}
?>